package com.efesco.inchar.list;

import java.util.Date;
import java.util.Calendar;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.data.*;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;

/**
 * InCharListViewModel
 */
public class InCharListViewModel extends DefaultViewModel {
  public void receiveMaterial(ParameterSet parameters)
      throws Exception {
    Dataset datasetMaterialReceive = getDataset("datasetMaterialReceive");

    DBStatement stmt = new DBStatement();
    try {
      datasetMaterialReceive.moveFirst();
      while (!datasetMaterialReceive.isLast()) {
        long receiveId = datasetMaterialReceive.getLong("IN_CHAR_MATERIAL_RECEIVE_ID");
        String status = datasetMaterialReceive.getString("STATUS");
        Date rDate = datasetMaterialReceive.getDate("RECEIVE_DATE");
        String receiveDate;
        if (rDate != null) {
          Calendar ca = Calendar.getInstance();
          ca.setTime(rDate);
          receiveDate = "TO_DATE('" + ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1) + 
          "-" + ca.get(Calendar.DATE) + "', 'YYYY-MM-DD')";
        } else {
          receiveDate = "NULL";
        }
    
        stmt.setSql("UPDATE S_IN_CHAR_MATERIAL_RECEIVE SET STATUS = " + status + ", RECEIVE_DATE = " + receiveDate + 
        " WHERE IN_CHAR_MATERIAL_RECEIVE_ID = " + receiveId);
        stmt.execute();
        
        datasetMaterialReceive.moveNext();
      }
    }
    finally {
      stmt.close();
    }
  }

}
